//
//  CalculatorViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CalculatorViewController.h"




@interface CalculatorViewController ()


@end

@implementation CalculatorViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    // **********              计算器
    
    // Do any additional setup after loading the view from its nib.
    
    operatorStr_=@"+-*/";
    expression_=[NSMutableString stringWithCapacity:30];
    inputString_=[NSMutableString stringWithCapacity:30];
    _inputStack_=[[TSStack alloc]init];
    _numbersStack_=[[TSStack alloc]init];
    _operatorStack_=[[TSStack alloc]init];
    
}

-(BOOL)isStackTopOperator:(TSStack *)stack{
    NSString *topStr=[NSString stringWithString:[stack peek]];
    NSRange childRange= [operatorStr_ rangeOfString:topStr];
    bool isOperator=(childRange.location!=NSNotFound);
    return isOperator;
}

- (void)pushOperator:(NSString *)btnText
{
    
    if (inputString_.length>0) {
        [_inputStack_ push:inputString_];
    }
    if (_inputStack_.count>0) {
        bool isOperator=[self isStackTopOperator:_inputStack_];
        if (isOperator) {
            [_inputStack_ pop];
        }
    }
    [inputString_ deleteCharactersInRange:NSMakeRange(0, inputString_.length)];
    [_inputStack_ push:btnText];
}

-(BOOL)canCaculate:(NSString *)currentOperator compareWith:(NSString *)stackTopOperator{
    if ([currentOperator isEqualToString:@"*"]) {
        if ([stackTopOperator isEqualToString:@"/"]) {
            return NO;
        }
        else{
            return YES;
        }
    }
    if ([currentOperator isEqualToString:@"/"]) {
        if ([stackTopOperator isEqualToString:@"/"]||[stackTopOperator isEqualToString:@"*"]) {
            return NO;
        }
        else{
            return YES;
        }
    }
    if ([currentOperator isEqualToString:@"+"]) {
        if ([stackTopOperator isEqualToString:@"+"]) {
            return YES;
        }
        else{
            return NO;
        }
    }
    if ([currentOperator isEqualToString:@"-"]) {
        if ([stackTopOperator isEqualToString:@"+"]) {
            return YES;
        }
        else{
            return NO;
        }
    }
    return NO;
}



- (IBAction)onceButton:(id)sender {

    UIButton *clickedBtn=(UIButton *)sender;
    NSString *btnText= clickedBtn.titleLabel.text;
    
    if([btnText isEqualToString:@"+"]){
        [self pushOperator:btnText];
    }
    else if([btnText isEqualToString:@"-"]){
        [self pushOperator:btnText];
    }
    else if([btnText isEqualToString:@"*"]){
        [self pushOperator:btnText];
    }
    else if([btnText isEqualToString:@"/"]){
        [self pushOperator:btnText];
        
    }
    else if ([btnText isEqualToString:@"="]){
        
        if (_inputStack_.count>1) {
            
            bool isOperator=[self isStackTopOperator:_inputStack_];
            if (isOperator) {
                if (inputString_.length>0) {
                    [_inputStack_ push:inputString_];
                    [inputString_ deleteCharactersInRange:NSMakeRange(0, inputString_.length)];
                    
                    [_finalExpressionLabel setText:_inputStack_.allElementString];
                    
                    while (_inputStack_.count>0) {
                        
                        if (_operatorStack_.count>0&&_numbersStack_.count>1) {
                            NSString *popStr=[_inputStack_ peek];
                            bool istopOperator=[self isStackTopOperator:_inputStack_];
                            if (istopOperator) {
                                bool caculateable=[self canCaculate:[_operatorStack_ peek] compareWith:popStr];
                                if (caculateable) {
                                    [self CaculateNumber];
                                }
                                else{
                                    NSString *cannotCaculate=[_inputStack_ pop];
                                    [_operatorStack_ push:cannotCaculate];
                                }
                            }
                            else{
                                NSString *popStr= [_inputStack_ pop];
                                [_numbersStack_ push:popStr];
                                //inputStack used up
                                if (_inputStack_.count==0) {
                                    while (_numbersStack_.count>1) {
                                        [self CaculateNumber];
                                    }
                                }
                            }
                            
                        }
                        else{
                            bool istopOperator=[self isStackTopOperator:_inputStack_];
                            if (istopOperator) {
                                NSString *popStr= [_inputStack_ pop];
                                [_operatorStack_ push:popStr];
                            }
                            else{
                                NSString *popStr= [_inputStack_ pop];
                                [_numbersStack_ push:popStr];
                            }
                            //inputStack used up
                            if (_inputStack_.count==0) {
                                while (_numbersStack_.count>1) {
                                    [self CaculateNumber];
                                }
                            }
                        }
                    }
                    
                }
            }
        }
        
    }
    else if([btnText isEqualToString:@"c"]){
        [_inputStack_ clear];
        [inputString_ deleteCharactersInRange:NSMakeRange(0, inputString_.length)];
        
    }
    else if([btnText isEqualToString:@"Delete"]){
        
        if (inputString_.length>0) {
            NSRange deleteRange=NSMakeRange(inputString_.length-1, 1);
            [inputString_ deleteCharactersInRange:deleteRange];
        }
        else {
            if (_inputStack_.count>0) {
                [inputString_ appendString:[_inputStack_ pop]];
                if (inputString_.length>0) {
                    NSRange deleteRange=NSMakeRange(inputString_.length-1, 1);
                    [inputString_ deleteCharactersInRange:deleteRange];
                }
                
            }
        }
    }
    else if([btnText isEqualToString:@"."]){
        if (inputString_.length>0) {
            NSRange childRange= [inputString_ rangeOfString:btnText];
            if (childRange.location==NSNotFound) {
                [inputString_ appendString:btnText];
            }
            
        }
        
    }
    else if([btnText isEqualToString:@"0"]){
        
        if (inputString_.length>0) {
            [inputString_ appendString:btnText];
        }
    }
    else{
        if (_inputStack_.count>0) {
            if (![self isStackTopOperator:_inputStack_]&&inputString_.length==0) {
                [_inputStack_ clear];
            }
        }
        [inputString_ appendString:btnText];
    }
    if (expression_.length>0) {
        NSRange expressionRange=NSMakeRange(0, expression_.length);
        [expression_ deleteCharactersInRange:expressionRange];
    }
    if (_numbersStack_.count==1&&_inputStack_.count==0&&_operatorStack_.count==0) {
        [_inputStack_ push:[_numbersStack_  pop]];
    }
    if (_inputStack_.count>0) {
        [expression_ appendString:_inputStack_.allElementString];
    }
    if (inputString_.length>0) {
        [expression_ appendString:inputString_];
    }
    
    if (![btnText isEqualToString:@"="]) {
        [_finalExpressionLabel setText:@""];
    }
    
    [_InputLabel setText:expression_];

    
}

- (void)CaculateNumber
{
    //caculate here
    double number1=[[_numbersStack_ pop] doubleValue];
    double number2=[[_numbersStack_ pop] doubleValue];
    double resultNumber=0;
    
    NSString *caculateOperator=[_operatorStack_ pop];
    if ([caculateOperator isEqualToString:@"+"]) {
        resultNumber=number1+number2;
    }
    else if ([caculateOperator isEqualToString:@"-"]) {
        resultNumber=number1-number2;
    }
    else if ([caculateOperator isEqualToString:@"*"]) {
        resultNumber=number1*number2;
    }
    else if ([caculateOperator isEqualToString:@"/"]) {
        resultNumber=number1/number2;
    }
    if (resultNumber!=0) {
        
        NSString *resultStr=[NSString stringWithFormat:@"%.3f",resultNumber];
        
        [_numbersStack_ push:resultStr];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationItem.title = _Calculator;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
}
- (void) leftAction {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}



@end
